#include "Client.h"
#include "Window.h"
Client *Client::p_Client = NULL;


Client::Client(HINSTANCE hInstance, int nCmdShow,Config *cfg){
p_Client = this;
Screen = Window(hInstance,nCmdShow);
settings = cfg;
Connect(settings->HOST_URL);
}


void Client::RunFrame(int time){

InvalidateRect(Window::s_hWnd,NULL,NULL);
};

bool Client::Connect(string URL){return false;}

LRESULT CALLBACK Client::HandleEvents(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
static HDC hdc;
static HGLRC hrc;

	switch (uMsg){
	case WM_CREATE:
	hdc = GetDC(hwnd);
	PIXELFORMATDESCRIPTOR pfd;
    int pixelFormat;
	memset(&pfd,0,sizeof(PIXELFORMATDESCRIPTOR));
	pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
	pfd.nVersion = 1;
	pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
	pfd.iPixelType = PFD_TYPE_RGBA;
	pfd.cColorBits = 32;
	pfd.cDepthBits = 16;
	pfd.iLayerType = PFD_MAIN_PLANE;
	pixelFormat = ChoosePixelFormat(hdc,&pfd);
	SetPixelFormat(hdc,pixelFormat,&pfd);
	hrc = wglCreateContext(hdc);
	wglMakeCurrent(hdc,hrc);
	glewInit();
	Window::s_hWnd = hwnd;
	g.Initialize();
	return 0;

	case WM_SETFOCUS:
	wglMakeCurrent(hdc,hrc);
	//GameActivate();
	return 0;

	case WM_KILLFOCUS:
	//GameDeactivate();
	return 0;

	case WM_PAINT:
	PAINTSTRUCT ps;
	hdc = BeginPaint(hwnd, &ps);
    g.Paint(hdc);
    EndPaint(hwnd, &ps);
    return 0;

    case WM_KEYDOWN:
    //state.StateChange(wParam,true);
    return 0;

    case WM_KEYUP:
    //state.StateChange(wParam,false);
    return 0;

    case WM_MOUSEMOVE:
    //state.MouseMove(wParam,lParam);
    return 0;

    case WM_LBUTTONDOWN:
    //state.MouseClick_L(wParam,lParam,true);
    return 0;

    case WM_LBUTTONUP:
    //state.MouseClick_L(wParam,lParam,false);
    return 0;

    case WM_RBUTTONDOWN:
    //state.MouseClick_R(wParam,lParam,true);
    return 0;

    case WM_RBUTTONUP:
    //state.MouseClick_R(wParam,lParam,false);
    return 0;

    case WM_SIZE:
    return 0;

	case WM_DESTROY:
	wglMakeCurrent(hdc,NULL);
	wglDeleteContext(hrc);
    ReleaseDC (hwnd, hdc);
    PostQuitMessage(0);
	return 0;

	}
	return DefWindowProc(hwnd,uMsg,wParam,lParam);
}